<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="title" :visible="showDialog" @close="btnCancel">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form
      v-if="showDialog"
      ref="form"
      label-width="120px"
      :model="formData"
      :rules="rules"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="formData.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="formData.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="formData.manager"
          style="width: 80%"
          placeholder="请选择"
          @focus="getEmployeeSimple"
        >
          <el-option
            v-for="item in people"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="formData.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          resize="none"
          :rows="3"
        />
      </el-form-item>
    </el-form>

    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import {
  getDepartments,
  addDepartments,
  getDepartmentDetails,
  editDepartment
} from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      required: true
    }
  },
  data() {
    const validateDeptsName = (rule, value, callback) => {
      getDepartments().then((data) => {
        const { depts } = data
        if (this.formData.id) {
          // 编辑
          depts.some(
            (item) =>
              item.id !== this.formData.id &&
                item.name === value &&
                // 编辑时 data 是自己
                // 所以 item.pid == 我自己的父部门 id 就是相同
                item.pid === this.data.pid
          )
            ? callback(new Error('同一个部门下不能重名'))
            : callback()
        } else {
          // 新增
          depts.some(
            (item) =>
              item.name === value &&
              // 新增时 data 是父部门
              // 所以找到一个 pid 跟 data.id 相同, 就是跟我同级
              item.pid === this.data.id
          )
            ? callback(new Error('同一个部门下不能重名'))
            : callback()
        }
      })
    }

    const validateDeptsCode = (rule, value, callback) => {
      getDepartments().then((data) => {
        const { depts } = data
        if (this.formData.id) {
          depts.some(
            (item) =>
              item.id !== this.formData.id && item.code === value && value
          )
            ? callback(new Error('不能有重复的部门代码'))
            : callback()
        } else {
          depts.some((item) => item.code === value && value)
            ? callback(new Error('不能有重复的部门代码'))
            : callback()
        }
      })
    }
    return {
      // 员工的列表
      people: [],
      formData: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门名称要求1-50个字符',
            trigger: 'blur'
          },
          { trigger: 'blur', validator: validateDeptsName }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门编码要求1-50个字符',
            trigger: 'blur'
          },
          { trigger: 'blur', validator: validateDeptsCode }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '介绍要求1-300个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  methods: {
    async getEmployeeSimple() {
      this.people = await getEmployeeSimple()
      console.log(this.people)
    },
    async btnOk() {
      try {
        // 校验表单
        const isValid = await this.$refs.form.validate()
        if (isValid) {
          // 根据当前状态区分到底是编辑还是新增
          if (this.formData.id) {
            // 这里是编辑逻辑
            await editDepartment(this.formData)
          } else {
            // 这里是新增逻辑
            const data = { ...this.formData, pid: this.data.id }
            await addDepartments(data)
          }

          // 修改完毕
          this.$emit('update:showDialog', false)
          this.$emit('addDepts')
        }
      } catch (error) {
        console.log(error)
      }
    },
    btnCancel() {
      // 强制重置数组, 即使是回显的其他如 id 之类的数据都应该取消
      this.formData = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      // 之前的重置还是不能少的, 因为还需要重置标摊错误提示
      // 之前只是隐藏了弹窗, 但是其实应该在隐藏之前
      // 将整个表单重置才对, 饿了么组件库已经封装好了一个函数
      this.$refs.form.resetFields()
      this.$emit('update:showDialog', false)
    },
    async getDepartmentDetails() {
      // 这里是回显函数
      // 最简单的是直接将外面传进来的回显
      // this.formData = this.data
      // 更加准确应该是重新获取
      const data = await getDepartmentDetails(this.data.id)
      this.formData = data
    }
  }
}
</script>

<style>
</style>
